package com.love.marriage.config;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
 * 微信支付通用配置
 */
@Configuration
@EnableConfigurationProperties({MinioProperties.class})
public class WxPayConfiguration implements InitializingBean {

    @Autowired
    private WeChatConfig weChatConfig;

    @Override
    public void afterPropertiesSet() {
        String appId = weChatConfig.getAppId();
        Assert.notNull(appId, "WeChat appId can't be null!");
    }

    /**
     * RSA自动证书配置（通用）
     */
    @Bean
    public RSAAutoCertificateConfig getConfig(){
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(weChatConfig.getMchid())
                .privateKey(weChatConfig.getPrivateKey())
                .merchantSerialNumber(weChatConfig.getMchSerialNumber())
                .apiV3Key(weChatConfig.getApiv3())
                .build();
    }
}
